package com.td.evo.log.cross;

import com.krakers.evo.log.EvolutionLogic;
import com.krakers.evo.obj.DNA;
import com.td.core.obj.TDMap;

public class TDCrosserLogic implements TDMap {

	private static final int S1 = BLD_X/2 * BLD_Y/2;
	
	public static void onePoint(DNA p1, DNA p2) {
		int indx = EvolutionLogic.r.nextInt(S1);
		DNA c1 = new DNA(p1.getSize());
		DNA c2 = new DNA(p2.getSize());
		p1.copyChain(c1, 0, indx);
		p2.copyChain(c2, 0, indx);
		p1.copyChain(c2, indx, p1.getSize());
		p2.copyChain(c1, indx, p2.getSize());
		c1.copyChain(p1);
		c2.copyChain(p2);
	}
	
	public static void twoPoint(DNA p1, DNA p2) {
		int indx1 = EvolutionLogic.r.nextInt(S1-1);
		int indx2 = indx1 + 1 + EvolutionLogic.r.nextInt(S1-1-indx1);
		DNA c1 = new DNA(p1.getSize());
		DNA c2 = new DNA(p2.getSize());
		p1.copyChain(c1, 0, indx1);
		p2.copyChain(c2, 0, indx1);
		p1.copyChain(c2, indx1, indx2);
		p2.copyChain(c1, indx1, indx2);
		p1.copyChain(c1, indx2, p1.getSize());
		p2.copyChain(c2, indx2, p2.getSize());
		c1.copyChain(p1);
		c2.copyChain(p2);
	}
	
	public static void mixOnePoint(DNA p1, DNA p2) {
		int indx = EvolutionLogic.r.nextInt(S1);
		DNA c1 = new DNA(p1.getSize());
		DNA c2 = new DNA(p2.getSize());
		p1.copyChain(c1, 0, indx);
		p2.copyChain(c2, 0, indx);
		p1.copyChain(c2, indx, S1);
		p2.copyChain(c1, indx, S1);
		
		//mix
		for(int i = 0; i < S1; i+=2) {
			c1.setGene(S1+i, p1.getGene(S1+i));
			c2.setGene(S1+i, p2.getGene(S1+i));
		}
		
		for(int i = 1; i < S1; i+=2) {
			c2.setGene(S1+i, p1.getGene(S1+i));
			c1.setGene(S1+i, p2.getGene(S1+i));
		}
		
		c1.copyChain(p1);
		c2.copyChain(p2);
	}
	
	public static void mixTwoPoint(DNA p1, DNA p2) {
		int indx1 = EvolutionLogic.r.nextInt(S1-1);
		int indx2 = indx1 + 1 + EvolutionLogic.r.nextInt(S1-1-indx1);
		DNA c1 = new DNA(p1.getSize());
		DNA c2 = new DNA(p2.getSize());
		p1.copyChain(c1, 0, indx1);
		p2.copyChain(c2, 0, indx1);
		p1.copyChain(c2, indx1, indx2);
		p2.copyChain(c1, indx1, indx2);
		p1.copyChain(c1, indx2, S1);
		p2.copyChain(c2, indx2, S1);
		
		//mix
		for(int i = 0; i < S1; i+=2) {
			c1.setGene(S1+i, p1.getGene(S1+i));
			c2.setGene(S1+i, p2.getGene(S1+i));
		}
		
		for(int i = 1; i < S1; i+=2) {
			c2.setGene(S1+i, p1.getGene(S1+i));
			c1.setGene(S1+i, p2.getGene(S1+i));
		}
		
		c1.copyChain(p1);
		c2.copyChain(p2);
	}
}
